package com.sailfish.springbootdemo.dao.db2;

import com.sailfish.springbootdemo.common.DB2TableName;
import com.sailfish.springbootdemo.pojo.db2.PcbConfig;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface PcbConfigDao {

    @Results(id = "pcbConfigResultMap", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER),
            @Result(column = "attr_name", property = "attrName", jdbcType = JdbcType.INTEGER),
            @Result(column = "attr_value", property = "attrValue", jdbcType = JdbcType.VARCHAR),
            @Result(column = "attr_value_en", property = "attrValueEn", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_default", property = "isDefault", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_user", property = "createUser", jdbcType = JdbcType.VARCHAR),
            @Result(column = "update_user", property = "updateUser", jdbcType = JdbcType.VARCHAR),
    })

    /*
     *查询单个
     */
    @Select("<script> " +
            "select id, attr_name, attr_value, attr_value_en, is_default, remark, sort, status, create_time, update_time, create_user, update_user " +
            " from " + DB2TableName.PCB_CONFIG +
            " where id = #{id}" +
            "</script>")
    PcbConfig getById(@Param("id") Integer id);

    /*
     *查询指定行数据
     */
    @Select("<script> " +
            "select id, attr_name, attr_value, attr_value_en, is_default, remark, sort, status, create_time, update_time, create_user, update_user " +
            "from " + DB2TableName.PCB_CONFIG +
            "<where>" +
//            "<if test='id != null'>" +
//            "    and id = #{id}" +
//            "</if>" +
            "<if test='attrName != null and attrName != \"\"'>" +
            "    and attr_name = #{attrName}" +
            "</if>" +
            "<if test='attrValue != null and attrValue != \"\"'>" +
            "    and attr_value = #{attrValue}" +
            "</if>" +
            "<if test='remark != null and remark != \"\"'>" +
            "    and remark = #{remark}" +
            "</if>" +
//            "<if test='sort != null'>" +
//            "    and sort = #{sort}" +
//            "</if>" +
            "<if test='status != null'>" +
            "    and status = #{status}" +
            "</if>" +
//            "<if test='createTime != null'>" +
//            "    and create_time = #{createTime}" +
//            "</if>" +
//            "<if test='updateTime != null'>" +
//            "    and update_time = #{updateTime}" +
//            "</if>" +
//            "<if test='createUser != null and createUser != \"\"'>" +
//            "    and create_user = #{createUser}" +
//            "</if>" +
//            "<if test='updateUser != null and updateUser != \"\"'>" +
//            "    and update_user = #{updateUser}" +
//            "</if>" +
            "</where>" +
            " order by sort" +
//            "limit #{pageable.offset}, #{pageable.pageSize}"+
            "</script>")

    @ResultMap(value = "pcbConfigResultMap")
    List<PcbConfig> getAllByLimit(String attrName, String attrValue, String remark, Integer status);

    @Select("<script> " +
            "select id, attr_name, attr_value, attr_value_en, is_default, remark, sort, status, create_time, update_time, create_user, update_user " +
            "from " + DB2TableName.PCB_CONFIG +
            "<where>" +
            "<if test='attr_name != null and attr_name != \"\"'>" +
            "    and attr_name LIKE CONCAT('%', #{attr_name}, '%')" +
            "</if>" +
            "</where>" +
            "</script>")
    @ResultMap(value = "pcbConfigResultMap")
    List<PcbConfig> getBulletins(@Param("attr_name") String attr_name);
    /*
     *统计总行数
     */
    @Select("<script> " +
            "select count(1) " +
            "from " + DB2TableName.PCB_CONFIG +
            "</script>")
    Integer getAllCount();

    /*
     *新增所有列
     */
    @Insert("insert into " + DB2TableName.PCB_CONFIG + " (attr_name, attr_value, attr_value_en, is_default, remark, sort, status, create_time, update_time, create_user, update_user) values " +
            "(#{attrName}, #{attrValue}, #{attrValueEn}, #{isDefault}, #{remark}, #{sort}, #{status}, #{createTime}, #{updateTime}, #{createUser}, #{updateUser})")
    @Options(useGeneratedKeys = true, keyProperty = "pcbSonFactoryId")
    Integer insert(PcbConfig param);

    /*
     *批量新增
     */
    @Insert("insert into " + DB2TableName.PCB_CONFIG + " ( attr_name, attr_value, attr_value_en, is_default, remark, sort, status, create_time, update_time, create_user, update_user) values " +
            "<foreach collection=\"entities\" item=\"entity\" separator=\",\">" +
            "(#{entity.attrName}, #{entity.attrValue}, #{entity.attrValueEn}, #{entity.isDefault}, #{entity.remark}, #{entity.sort}, #{entity.status}, #{entity.createTime}, #{entity.updateTime}, #{entity.createUser}, #{entity.updateUser})")
    Integer insertBatch(@Param("entities") List<PcbConfig> entities);


    /*
     *通过主键删除
     */
    @Delete("delete from " + DB2TableName.PCB_CONFIG + " where id = #{id} ")
    Integer delete(@Param("id") Integer id);

    @Delete("delete from " + DB2TableName.PCB_CONFIG + " where  id in " +
            "<foreach collection=\"ids\" item = \"id\" open=\"(\" separator=\",\" close=\")\">  " +
            "#{id} " +
            "</foreach>"
    )
    Integer deleteBatch(@Param("ids") List<Integer> ids);

    /*
     *通过主键修改数据
     */
    @Update("<script>" +
            "update " + DB2TableName.PCB_CONFIG +
            "<set>" +
            "<if test='attrName != null and attrName != \"\"'>" +
            "attr_name = #{attrName}," +
            "</if>" +
            "<if test='attrValue != null and attrValue != \"\"'>" +
            "    attr_value = #{attrValue}," +
            "</if>" +
            "<if test='attrValueEn != null and attrValueEn != \"\"'>" +
            "    attr_value_en = #{attrValueEn}," +
            "</if>" +
            "<if test='isDefault != null and isDefault != \"\"'>" +
            "    is_default = #{isDefault}," +
            "</if>" +
            "<if test='remark != null and remark != \"\"'>" +
            "    remark = #{remark}," +
            "</if>" +
            "<if test='sort != null'>" +
            "    sort = #{sort}," +
            "</if>" +
            "<if test='status != null'>" +
            "    status = #{status}," +
            "</if>" +
            "<if test='createTime != null'>" +
            "    create_time = #{createTime}," +
            "</if>" +
            "<if test='updateTime != null'>" +
            "    update_time = #{updateTime}," +
            "</if>" +
            "<if test='createUser != null and createUser != \"\"'>" +
            "    create_user = #{createUser}," +
            "</if>" +
            "<if test='updateUser != null and updateUser != \"\"'>" +
            "    update_user = #{updateUser}," +
            "</if>" +
            "</set>" +
            "where id = #{id}" +
            "</script>")
    Integer update(PcbConfig param);
}
